<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>FAR分析推荐系统 | 注册</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome -->
    <link rel="stylesheet" href="/static/plugins/fontawesome-free/css/all.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <!-- icheck bootstrap -->
    <link rel="stylesheet" href="/static/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="/static/dist/css/adminlte.min.css">
    <!-- Google Font: Source Sans Pro -->
    <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
</head>
<body class="hold-transition register-page">
<div class="register-box">
    <div class="register-logo">
        <a href="{{ url_for('login') }}"><b>FAR</b>分析推荐系统</a>
    </div>

    <div class="card">
        <div class="card-body register-card-body">
            <p class="login-box-msg">注册新用户</p>

            <form action="{{ url_for('account.user_register') }}" method="post">
                <div class="input-group mb-3">
                    <input type="text" class="form-control" placeholder="全名" name="username" value="{{ username }}">
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-user"></span>
                        </div>
                    </div>
                </div>
                <div class="input-group mb-3">
                    <input type="email" class="form-control" id="emailInput" placeholder="邮箱" name="email"
                           onchange="checkEmailValid();" value="{{ email }}">
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-envelope"></span>
                        </div>
                    </div>
                </div>
                <div class="input-group mb-3">
                    <input type="password" class="form-control" id="passwordInput1" placeholder="密码" name="password" value="{{ password }}">
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-lock"></span>
                        </div>
                    </div>
                </div>
                <div class="input-group mb-3">
                    <input type="password" class="form-control" id="passwordInput2" placeholder="确认密码" name="re_password" oninput="checkPswValid()" value="{{ re_password }}">
                    <div class="input-group-append">
                        <div class="input-group-text">
                            <span class="fas fa-lock"></span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-8">
                        <div class="icheck-primary">
                            <input type="checkbox" id="agreeTerms" name="terms" value="agree">
                            <label for="agreeTerms">
                                我同意 <a href="#">用户协议</a>
                            </label>
                        </div>
                    </div>
                    <!-- /.col -->
                    <div class="col-4">
                        <button type="submit" class="btn btn-primary btn-block">注册</button>
                    </div>
                    <!-- /.col -->
                </div>
            </form>
            <div class="row">
                <div class="col-8">
                    <a href="{{ url_for('login') }}" class="text-center">我已经有账号了</a>
                </div>
                <div class="col-4" style="color: #921925;font-size: small">
                    {{ error }}
                </div>
            </div>

        </div>
        <!-- /.form-box -->
    </div><!-- /.card -->
</div>
<!-- /.register-box -->

<!-- jQuery -->
<script src="/static/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="/static/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="/static/dist/js/adminlte.min.js"></script>
<script>
    function checkEmailValid() {
        //检查邮箱是否存在
        let email = $("#emailInput").val();
        jQuery.ajax({
            //几个参数需要注意一下
            type: "GET",//方法类型
            async: false,
            dataType: "json",//预期服务器返回的数据类型
            url: "{{ url_for('account.check_email_valid') }}"+"?email="+email,//url
            success: function (data) {
                if (data.valid !== 1) {
                    $('#emailInput').attr('class', 'form-control is-invalid');
                    alert("邮箱" + email + "已存在！");
                } else
                    $('#emailInput').attr('class', 'form-control is-valid');
            },
            error: function () {
                alert("异常！");
            }
        });
    }
</script>
<script>
    function checkPswValid(){
        //检查两次输入是否一致
        let psw1 = $("#passwordInput1").val();
        let psw2 = $("#passwordInput2").val();
        if(psw1 === "")
            $('#passwordInput1').attr('class', 'form-control is-invalid');
        else{
            $('#passwordInput1').attr('class', 'form-control is-valid');
            if(psw1 !== psw2)
                $('#passwordInput2').attr('class', 'form-control is-warning');
            else
                $('#passwordInput2').attr('class', 'form-control is-valid');
        }
    }
</script>
</body>
</html>
